package com.txc.books.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.txc.books.consts.FilingConstants;
import com.txc.books.consts.LoanConstants;
import com.txc.books.entity.LoanRecords;
import com.txc.books.mapper.LoanRecordsMapper;
import com.txc.books.service.ILoanRecordsService;
import com.txc.books.utils.CountUtils;
import com.txc.books.utils.PageRequest;
import com.txc.books.utils.PageResult;
import com.txc.books.utils.PageUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: XCTANG
 *
 */

@Service
public class LoanRecordsImpl implements ILoanRecordsService {

    private LoanRecordsMapper loanRecordsMapper;

    public LoanRecordsImpl(LoanRecordsMapper loanRecordsMapper){
        this.loanRecordsMapper = loanRecordsMapper;
    }


    @Override
    public List<LoanRecords> findAll() {
        return loanRecordsMapper.findAll();
    }

    @Override
    public void insert(LoanRecords loanRecords) {

        loanRecordsMapper.insert(loanRecords);
    }

    @Override
    public void update(LoanRecords loanRecords) {

        loanRecordsMapper.update(loanRecords);
    }

    @Override
    public void delete(Integer id) {

        loanRecordsMapper.delete(id);
    }

    @Override
    public void select(Integer id) {
        loanRecordsMapper.select(id);
    }

    @Override
    public void selectByBookName(String name) {
        loanRecordsMapper.selectByBookName(name);
    }

    @Override
    public void selectByMemberName(String name) {
        loanRecordsMapper.selectByMemberName(name);
    }

    @Override
    public void selectByTime(String createTime) {
        loanRecordsMapper.selectByTime(createTime);
    }

    @Override
    public void selectByStauts(Integer loanStauts) {
        loanRecordsMapper.selectByStauts(loanStauts);
    }

    @Override
    public void selectLoan(Integer id) {

        LoanRecords loanRecords = new LoanRecords();

        if (loanRecords.getLoanStauts() == LoanConstants.EARLY || loanRecords.getLoanStauts() == LoanConstants.ONTIME_R || loanRecords.getLoanStauts() == LoanConstants.OVERDUE_R)
            loanRecordsMapper.selectLoan(id);

    }

    @Override
    public void selectNoLoan(Integer id) {
        LoanRecords loanRecords = new LoanRecords();

        if (loanRecords.getLoanStauts() == LoanConstants.ONTIME_NOR || loanRecords.getLoanStauts() == LoanConstants.OVERDUE_NOR)
            loanRecordsMapper.selectNoLoan(id);
    }

    @Override
    public PageResult findByPager(PageRequest pageRequest) {

        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    public PageInfo<LoanRecords> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<LoanRecords> loanMenus = loanRecordsMapper.selectPage();
        return new PageInfo<LoanRecords>(loanMenus);
    }

    //借阅图书
    @Override
    public void borrowBook(LoanRecords loanRecords) {

        Integer id = loanRecords.getId();
        String c = loanRecords.getCreateTime();
        String e = loanRecords.getEndTime();
        String t = loanRecords.getTotal();
        List<LoanRecords> list = loanRecordsMapper.selectLoan(id);

        if (list != null && list.size() > 0){
            loanRecords.setLoanStauts(LoanConstants.ONTIME_NOR);
            loanRecords.setDataStauts(FilingConstants.OVER);
            loanRecords.setTotal(CountUtils.count(t,c,e));
        }
        loanRecordsMapper.insert(loanRecords);
    }

    //归还图书
    @Override
    public void returnBook(LoanRecords loanRecords) {

        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH");

        Integer id = loanRecords.getId();
        String c = loanRecords.getCreateTime();
        String et = loanRecords.getEndTime();
        String e = loanRecords.setEndTime(sd.format(new Date()));
        String t = loanRecords.getTotal();
        List<LoanRecords> list = loanRecordsMapper.selectNoLoan(id);
        if (list != null && list.size() > 0){

            if (CountUtils.compareTo(et,e) == "1"){

                loanRecords.setTotal(CountUtils.count(t,c,e));

                loanRecords.setLoanStauts(LoanConstants.EARLY);

            }else if (CountUtils.compareTo(et,e) == "0"){

                loanRecords.setTotal(CountUtils.count(t,c,e));

                loanRecords.setLoanStauts(LoanConstants.ONTIME_R);

            }else {

                loanRecords.setTotal(CountUtils.count1(t,c,e));

                loanRecords.setLoanStauts(LoanConstants.OVERDUE_R);

            }

            loanRecords.setDataStauts(FilingConstants.NOT_OVER);
        }

        loanRecordsMapper.update(loanRecords);
    }
}
